<!DOCTYPE html>



  

 
<script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>

<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="搭建框架时引入spring-boot-stater-websocket后可以正常运行，但所有单元测试都执行失败。出现以下错误：12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849Error starting ApplicationContext. To display th">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring Boot WebSocket单元测试异常解决方案">
<meta property="og:url" content="https://lichujun.gitee.io/blog/2018/09/14/Spring-Boot-WebSocket单元测试异常解决方案/index.html">
<meta property="og:site_name" content="十仔的博客">
<meta property="og:description" content="搭建框架时引入spring-boot-stater-websocket后可以正常运行，但所有单元测试都执行失败。出现以下错误：12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849Error starting ApplicationContext. To display th">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-09-17T00:51:28.618Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Spring Boot WebSocket单元测试异常解决方案">
<meta name="twitter:description" content="搭建框架时引入spring-boot-stater-websocket后可以正常运行，但所有单元测试都执行失败。出现以下错误：12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849Error starting ApplicationContext. To display th">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://lichujun.gitee.io/blog/2018/09/14/Spring-Boot-WebSocket单元测试异常解决方案/"/>





  <title>Spring Boot WebSocket单元测试异常解决方案 | 十仔的博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">十仔的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://lichujun.gitee.io/blog/blog/2018/09/14/Spring-Boot-WebSocket单元测试异常解决方案/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="lichujun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="十仔的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Spring Boot WebSocket单元测试异常解决方案</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-09-14T23:27:14+08:00">
                2018-09-14
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>搭建框架时引入spring-boot-stater-websocket后可以正常运行，但所有单元测试都执行失败。出现以下错误：<br><figure class="highlight maxima"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">Error starting ApplicationContext. To <span class="built_in">display</span> the conditions report re-run your application with 'debug' enabled.</span><br><span class="line"><span class="number">2018</span>-<span class="number">08</span>-<span class="number">23</span> <span class="number">19</span>:<span class="number">56</span>:<span class="number">18</span>,<span class="number">614</span> - Application run failed</span><br><span class="line">org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverEndpointExporter' defined <span class="keyword">in</span> class path resource [com/sell/config/WebSocketConfig.class]: Invocation of init <span class="built_in">method</span> failed; nested exception <span class="built_in">is</span> java.lang.IllegalStateException: javax.websocket.server.ServerContainer <span class="keyword">not</span> available</span><br><span class="line">    <span class="built_in">at</span> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:<span class="number">1699</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:<span class="number">573</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:<span class="number">495</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.beans.factory.support.AbstractBeanFactory.<span class="built_in">lambda</span>$doGetBean$<span class="number">0</span>(AbstractBeanFactory.java:<span class="number">317</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:<span class="number">222</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:<span class="number">315</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:<span class="number">199</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:<span class="number">759</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.<span class="built_in">context</span>.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:<span class="number">869</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.<span class="built_in">context</span>.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:<span class="number">550</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.boot.SpringApplication.refresh(SpringApplication.java:<span class="number">762</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:<span class="number">398</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.boot.SpringApplication.run(SpringApplication.java:<span class="number">330</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.boot.test.<span class="built_in">context</span>.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:<span class="number">139</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.test.<span class="built_in">context</span>.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:<span class="number">99</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.test.<span class="built_in">context</span>.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:<span class="number">117</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.test.<span class="built_in">context</span>.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:<span class="number">108</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.test.<span class="built_in">context</span>.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:<span class="number">190</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.test.<span class="built_in">context</span>.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:<span class="number">132</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.test.<span class="built_in">context</span>.TestContextManager.prepareTestInstance(TestContextManager.java:<span class="number">246</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.test.<span class="built_in">context</span>.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:<span class="number">227</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.test.<span class="built_in">context</span>.junit4.SpringJUnit4ClassRunner$<span class="number">1.</span>runReflectiveCall(SpringJUnit4ClassRunner.java:<span class="number">289</span>)</span><br><span class="line">    <span class="built_in">at</span> org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:<span class="number">12</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.test.<span class="built_in">context</span>.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:<span class="number">291</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.test.<span class="built_in">context</span>.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:<span class="number">246</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.test.<span class="built_in">context</span>.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:<span class="number">97</span>)</span><br><span class="line">    <span class="built_in">at</span> org.junit.runners.ParentRunner$<span class="number">3.</span>run(ParentRunner.java:<span class="number">290</span>)</span><br><span class="line">    <span class="built_in">at</span> org.junit.runners.ParentRunner$<span class="number">1.</span>schedule(ParentRunner.java:<span class="number">71</span>)</span><br><span class="line">    <span class="built_in">at</span> org.junit.runners.ParentRunner.runChildren(ParentRunner.java:<span class="number">288</span>)</span><br><span class="line">    <span class="built_in">at</span> org.junit.runners.ParentRunner.access$<span class="number">000</span>(ParentRunner.java:<span class="number">58</span>)</span><br><span class="line">    <span class="built_in">at</span> org.junit.runners.ParentRunner$<span class="number">2.</span>evaluate(ParentRunner.java:<span class="number">268</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.test.<span class="built_in">context</span>.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:<span class="number">61</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.test.<span class="built_in">context</span>.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:<span class="number">70</span>)</span><br><span class="line">    <span class="built_in">at</span> org.junit.runners.ParentRunner.run(ParentRunner.java:<span class="number">363</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.test.<span class="built_in">context</span>.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:<span class="number">190</span>)</span><br><span class="line">    <span class="built_in">at</span> org.junit.runner.JUnitCore.run(JUnitCore.java:<span class="number">137</span>)</span><br><span class="line">    <span class="built_in">at</span> com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:<span class="number">68</span>)</span><br><span class="line">    <span class="built_in">at</span> com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:<span class="number">47</span>)</span><br><span class="line">    <span class="built_in">at</span> com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:<span class="number">242</span>)</span><br><span class="line">    <span class="built_in">at</span> com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:<span class="number">70</span>)</span><br><span class="line">Caused by: java.lang.IllegalStateException: javax.websocket.server.ServerContainer <span class="keyword">not</span> available</span><br><span class="line">    <span class="built_in">at</span> org.springframework.util.Assert.state(Assert.java:<span class="number">73</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.web.socket.server.standard.ServerEndpointExporter.afterPropertiesSet(ServerEndpointExporter.java:<span class="number">106</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:<span class="number">1758</span>)</span><br><span class="line">    <span class="built_in">at</span> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:<span class="number">1695</span>)</span><br><span class="line">    ... <span class="number">39</span> common frames omitted</span><br></pre></td></tr></table></figure></p>
<p>通过观察发现控制台报错信息中没有启动tomcat容器。但是websocket是需要依赖tomcat等容器的启动。所以在测试过程中我们要真正的启动一个tomcat作为容器。修改测试类SpringBootTest注解，如下所示:<br><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@SpringBootTest(webEnvironment = SpringBootTest<span class="selector-class">.WebEnvironment</span><span class="selector-class">.RANDOM_PORT</span>)</span><br></pre></td></tr></table></figure></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/09/14/Spring-Boot中使用WebSocket/" rel="next" title="Spring Boot中使用WebSocket">
                <i class="fa fa-chevron-left"></i> Spring Boot中使用WebSocket
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/09/14/Mybatis批量插入和更新/" rel="prev" title="Mybatis批量插入和删除">
                Mybatis批量插入和删除 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">lichujun</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">32</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">lichujun</span>

  
</div>









        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
